package com.dingwen.treasure.web.config;

import com.dingwen.treasure.web.interceptor.ServerProtectInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * web mvc 配置
 *
 * @author dingwen
 * @date 2022/03/31
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    /**
     * 不拦截白名单 </br>
     */
    private  final List<String> WHITE_PATH_LIST = Arrays.asList(
            "/doc.html",
            "/webjars/**",
            "/swagger-resources/**"
    );

    /**
     * 微服务之间的拦截器
     */
    @Resource
    private  ServerProtectInterceptor serverProtectInterceptor;

    /**
     * knife4j 文档
     *
     * @param registry 注册表
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(serverProtectInterceptor)
                .order(Integer.MIN_VALUE)
                .addPathPatterns("/**")
                .excludePathPatterns(WHITE_PATH_LIST);
    }
}
